/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.mall.biz.shop.service.impl;


import co.yixiang.common.core.utils.FileUtil;
import co.yixiang.common.mproot.common.service.impl.BaseServiceImpl;
import co.yixiang.common.mproot.common.utils.QueryHelpPlus;
import co.yixiang.common.mproot.domain.PageResult;
import co.yixiang.common.mproot.dozer.service.IGenerator;
import co.yixiang.mall.biz.shop.mapper.YxStoreProductStockValueMapper;
import co.yixiang.mall.biz.shop.service.YxStoreProductStockValueService;
import co.yixiang.mall.common.shop.domain.YxStoreProductStockValue;
import co.yixiang.mall.common.shop.dto.YxStoreProductStockValueDto;
import co.yixiang.mall.common.shop.dto.YxStoreProductStockValueQueryCriteria;
import co.yixiang.mall.common.shop.dto.YxStoreProductStockVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
* @author zhuhongjian
* @date 2021-03-17
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxStoreProductStockValue")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreProductStockValueServiceImpl extends BaseServiceImpl<YxStoreProductStockValueMapper, YxStoreProductStockValue> implements YxStoreProductStockValueService {

    private final IGenerator generator;
    private final YxStoreProductStockValueMapper yxStoreProductStockValueMapper;


    @Override
    //@Cacheable
    public PageResult<YxStoreProductStockVo> queryAll(YxStoreProductStockValueQueryCriteria criteria, Pageable pageable) {
        Map<String,Object> params = new HashMap<>();
        params.put("id",criteria.getId());
        params.put("productName",criteria.getProductName());
        params.put("skuName",criteria.getSkuName());
        PageHelper.startPage(pageable.getPageNumber()+1, pageable.getPageSize(),null);
        List<YxStoreProductStockVo> yxStoreProductStockVos = yxStoreProductStockValueMapper.selectList(params);
        PageInfo<YxStoreProductStockVo> page = new PageInfo<>(yxStoreProductStockVos);
        return generator.convertPageInfo(page,YxStoreProductStockVo.class);
    }


    @Override
    //@Cacheable
    public List<YxStoreProductStockValue> queryAll(YxStoreProductStockValueQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxStoreProductStockValue.class, criteria));
    }


    @Override
    public void download(List<YxStoreProductStockValueDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxStoreProductStockValueDto yxStoreProductStockValue : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("商品ID", yxStoreProductStockValue.getProductId());
            map.put("产品规格属性", yxStoreProductStockValue.getSkuId());
            map.put("门店id", yxStoreProductStockValue.getStoreId());
            map.put("属性对应的库存", yxStoreProductStockValue.getStock());
            map.put("进货量", yxStoreProductStockValue.getPurchaseVolume());
            map.put("总进货单价", yxStoreProductStockValue.getPurchasePrice());
            map.put("进货合计金额", yxStoreProductStockValue.getPruchaseAmount());
            map.put("总销货数量", yxStoreProductStockValue.getSalesVolume());
            map.put("销货单价", yxStoreProductStockValue.getSalesPrice());
            map.put("销货合计金额", yxStoreProductStockValue.getSalesAmount());
            map.put("剩余数量", yxStoreProductStockValue.getLastVolume());
            map.put("剩余单价", yxStoreProductStockValue.getLastPrice());
            map.put("剩余合计金额", yxStoreProductStockValue.getLastAmount());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
